package com.ideafactory.client.views;


import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ideafactory.client.beans.CatalogGoodsBean;
import com.ideafactory.client.beans.ContantDatas;
import com.ideafactory.client.utils.TextVUtils;
import com.ideafactory.client.ybservice.APP;
import com.ideafactory.client.ybselfservice.R;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class RightGridViewAdapter extends RecyclerView.Adapter<RightGridViewAdapter.RightHolder> {
    private LayoutInflater mLayoutInflater;

    private boolean isPopAdapter = false;

    public void setIsPopAdapter(boolean isPopAdapter) {
        this.isPopAdapter = isPopAdapter;
    }

    public RightGridViewAdapter() {
        mLayoutInflater = LayoutInflater.from(APP.getAPP().getApplicationContext());
    }

    public interface OnItemClickLinster {
        void onItemClick(View view, int position);
    }

    private OnItemClickLinster onItemClickLinster;

    public void setOnItemClickLinster(OnItemClickLinster onItemClickLinster) {
        this.onItemClickLinster = onItemClickLinster;
    }

    private List<CatalogGoodsBean> catalogGoodsLists;

    public void setRightList(List<CatalogGoodsBean> catalogGoodsLists) {
        this.catalogGoodsLists = catalogGoodsLists;
        notifyDataSetChanged();
    }

    public void clearMdata() {
        catalogGoodsLists.clear();
        notifyDataSetChanged();
    }


    @Override
    public RightHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.inflate(R.layout.right_view_item, parent, false);
        RightHolder viewHolder = new RightHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final RightHolder holder, int position) {
        holder.setIsRecyclable(false);
        holder.setData(position, holder.itemView);
    }

    @Override
    public int getItemCount() {
        return catalogGoodsLists == null ? 0 : catalogGoodsLists.size();
    }

    class RightHolder extends RecyclerView.ViewHolder {
        ImageView goodsImage;
        ImageView left_des_image;
        ImageView right_des_image;
        TextView goodsName;
        TextView goodPrice;
        LinearLayout ll_right_view;

        public RightHolder(View itemView) {
            super(itemView);
            goodsImage = (ImageView) itemView.findViewById(R.id.right_view_item_image);
            left_des_image = (ImageView) itemView.findViewById(R.id.left_des_image);
            goodsName = (TextView) itemView.findViewById(R.id.right_view_name);
            goodPrice = (TextView) itemView.findViewById(R.id.right_view_money);
            right_des_image = (ImageView) itemView.findViewById(R.id.right_des_image);
            ll_right_view = (LinearLayout) itemView.findViewById(R.id.ll_right_view);
        }

        public void setData(int position, final View itemView) {

            if (!isPopAdapter) {
                itemView.setBackgroundResource(R.drawable.right_view_back);//正常
            }
            CatalogGoodsBean catalogGoodsBean = catalogGoodsLists.get(position);



            //设置新品图标
            if (catalogGoodsBean.getIsNew() == 1) {
                left_des_image.setImageResource(R.mipmap.good_new);
            }

            //设置打折图标
            if (catalogGoodsBean.getIsPromotion() == 1) {
                right_des_image.setImageResource(R.mipmap.good_discount);
            } else {

            }

            if (catalogGoodsBean.getCount() == 0 && catalogGoodsBean.getType() != 3) {
                right_des_image.setImageResource(R.mipmap.goods_sell_out);
                itemView.setBackgroundResource(R.drawable.right_view_sold_out_back);
            } else {
                if (onItemClickLinster != null) {
                    itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            int pos = getLayoutPosition();
                            onItemClickLinster.onItemClick(itemView, pos);
                        }
                    });
                }
            }

            goodsName.setText(catalogGoodsBean.getName());

            String unit = getUint(catalogGoodsBean.getUnit());



            //设置售卖方式（1单品/2套餐/3组合）
            if (catalogGoodsBean.getType() == 3) {
                goodPrice.setVisibility(View.GONE);
            } else if (catalogGoodsBean.getType() == 2) {
//                if (catalogGoodsBean.getDiscount()==0d){
//                    goodPrice.setText(TextVUtils.highlight("￥" + String.valueOf(catalogGoodsBean.getPrice()) + "/" + unit));
//                }else {
//                    goodPrice.setText(TextVUtils.setDeleteLine(catalogGoodsBean.getDiscount() + "", catalogGoodsBean.getPrice() + "", unit));
//                }

            } else {
                goodPrice.setText(TextVUtils.highlight(APP.getAPP().getResources().getString(R.string.money_unit)+ String.valueOf(catalogGoodsBean.getPrice()) + "/" + unit));
            }

            if (catalogGoodsBean.getDiscount()!=0d){
                goodPrice.setText(TextVUtils.setDeleteLine(catalogGoodsBean.getDiscount() + "", catalogGoodsBean.getPrice() + "", unit));
            }


            if (catalogGoodsBean.getImageSrc().size() > 0) {
                ImageLoader.getInstance().displayImage(catalogGoodsBean.getImageSrc().get(0), goodsImage, APP.getAPP().getImageOption());
            }

        }

    }


    private String getUint(int unitType) {
        return ContantDatas.unitString[unitType - 1];
    }


}
